﻿using Microsoft.Win32;
using System.Windows;
using SpeedRecognition.RecordModel;

namespace SpeedRecognition
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            WhisperManager.Instance.Init();
            WhisperManager.Instance.WriteWhisperInfo = SetText;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if(ofd.ShowDialog() == true )
            {
                WhisperManager.Instance.ReadFile(ofd.FileName);
            }
        }

        readonly NaudioRecordManager naudioRecordManager = new();
        private void SaveMicrophone(object sender, RoutedEventArgs e)
        {
            //naudioRecordManager.RecordStream = new RecordFileStream(naudioRecordManager.WaveFormat, "1.wav");
            RecordMemoryStream memoryStream = new RecordMemoryStream(naudioRecordManager.WaveFormat)
                {
                    WaveStreamCallBack = data =>
                    { 
                        //File.WriteAllBytes("44.wav",data);
                        WhisperManager.Instance.ReadData(data);
                    }
                };
            naudioRecordManager.RecordStream = memoryStream;
            naudioRecordManager.SetMicrophoneDevice();
            naudioRecordManager.StartRecordAudio();
        }

        private void SaveLoudSpeaker(object sender, RoutedEventArgs e)
        {
            RecordMemoryStream memoryStream = new RecordMemoryStream(naudioRecordManager.WaveFormat)
                {
                    WaveStreamCallBack = data =>
                    {
                        //File.WriteAllBytes("44.wav", data);
                        WhisperManager.Instance.ReadData(data);
                    }
                };
            naudioRecordManager.RecordStream = memoryStream;
            //naudioRecordManager.RecordStream = new RecordFileStream(naudioRecordManager.WaveFormat, "2.wav");
            naudioRecordManager.SetLoudSpeakerDevice();
            naudioRecordManager.StartRecordAudio();
           
        }

        private void SetText(TimeSpan s , TimeSpan e, string t)
        {
            this.txt.Dispatcher.Invoke(() =>
            {
                this.txt.AppendText($"{s}->{e}:{t}{Environment.NewLine}");
                this.txt.ScrollToEnd();
            });
        }

        private void StopRecord(object sender, RoutedEventArgs e)
        {
            naudioRecordManager.StopRecordAudio(); 
        }
    }
}